package com.xrz.sxm.aj.utils;

import android.os.Environment;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import de.mindpipe.android.logging.log4j.LogConfigurator;

public class Log2File {

	static private final String PATH = Environment.getExternalStorageDirectory() + File.separator + "艾邻log"
			+ File.separator + "logs" + File.separator;
	
	private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

	private static Log2File log2File;

	public static Log2File newInstant() {
		if (log2File == null)
			log2File = new Log2File();
		return log2File;
	}

	private Log2File() {
		configLog();
	}

	private Logger gLogger;

	private void configLog() {
		File file = new File(PATH);
		if(file.exists() == false)
			file.mkdirs();
		final LogConfigurator logConfigurator = new LogConfigurator();

		logConfigurator.setFileName(PATH +  format.format(new Date())+".log");
		// Set the root log level
		logConfigurator.setRootLevel(Level.DEBUG);
		// Set log level of a specific logger
		logConfigurator.setLevel("org.apache", Level.ERROR);
		logConfigurator.configure();

		// gLogger = Logger.getLogger(this.getClass());
		gLogger = Logger.getLogger("CrifanLiLog4jTest");
	}

	public void log2File(String msg) {

		try {
			File file = new File(PATH);
			if (file.exists() && file.length() > 10 * 1024 * 1000) {
				// 如果文件大于10M的话，就删除掉
				file.delete();
			}
		} catch (Exception e) {
		}

		gLogger.debug(msg);
	}
}
